# -- coding: utf-8 --

# 所有的工具类都应该是类方法
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


class DBTools:

    @classmethod
    def get_conn(cls):
        import pymysql
        return pymysql.connect(host='192.168.44.128', user='root', password='123456', database='woniuboss4.0')

    @classmethod
    def update(cls, sql):
        conn = cls.get_conn()
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()

    @classmethod
    def query(cls, sql):
        conn = cls.get_conn()
        cursor = conn.cursor()
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        return result


class UITools:

    driver = None

    @classmethod
    def get_driver(cls, browser):
        from selenium import webdriver
        if hasattr(webdriver, browser):
            cls.driver = getattr(webdriver, browser)()
        else:
            cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()
        return cls.driver

    @classmethod
    def get_element(cls, locator):  # {"by":"CSS_SELECTOR", "what":"xxxx"}
        """
        根据定位方式来查找元素对象，如果找不到返回None
        :param locator:
        :return:
        """
        by = locator['by']
        what = locator['what']
        try:
            return cls.driver.find_element(getattr(By, by), what)  # 找到了元素
        except Exception as e:
            return None

    @classmethod
    def input(cls, locator, value):
        """
        向文本框元素输入内容
        :param locator: 定位方式
        :param value: 要输入的值
        :return: None
        """
        element = cls.get_element(locator)
        element.click()
        element.clear()
        element.send_keys()

    @classmethod
    def click(cls, locator):
        """
        点击某个元素
        :param locator:元素定位方式
        :return: None
        """
        element = cls.get_element(locator)
        element.click()

class TestCase:

    driver = UITools.get_driver('Firefox')

    def setup(self):
        driver =self.driver
        driver.get('http://192.168.44.128:8080/WoniuBoss4.0')
        driver.find_element(By.NAME, 'userName').send_keys('WNXA088')
        driver.find_element(By.NAME, 'userPass').send_keys('Woniu.123456')
        driver.find_element(By.CSS_SELECTOR, '.btn').click()

    def test_add_assets(self):
        self.setup()
        driver = self.driver
        driver.get('http://192.168.44.128:8080/WoniuBoss4.0/assets')

        sql = 'SELECT count(assets_id) from assets'
        old = DBTools.query(sql)  # 获得没有添加设置之前的记录数

        driver.find_element(By.CSS_SELECTOR, 'button.btn-padding:nth-child(1)').click()

        assets_name = driver.find_element(By.CSS_SELECTOR,
                                          '#addAss-form > div:nth-child(1) > div:nth-child(1) > '
                                          'div:nth-child(1) > div:nth-child(1) > select:nth-child(2)')
        asserts_size = len(Select(assets_name).options)
        import random
        random_index = random.randint(1, asserts_size - 1)
        Select(assets_name).select_by_index(random_index)
        # 设备类型
        assets_type = driver.find_element(By.CSS_SELECTOR, 'select.form-control:nth-child(1)')
        type_size = len(Select(assets_type).options)
        random_index = random.randint(1, type_size - 1)
        Select(assets_type).select_by_index(random_index)

        # 条码
        barcode = driver.find_element(By.CSS_SELECTOR,
                                      '#addAss-form > div:nth-child(1) > div:nth-child(1) > '
                                      'div:nth-child(2) > div:nth-child(1) > input:nth-child(2)')
        barcode.send_keys('45234524528')

        price = driver.find_element(By.CSS_SELECTOR,
                                    '#addAss-form > div:nth-child(1) > div:nth-child(1) > '
                                    'div:nth-child(2) > div:nth-child(2) > input:nth-child(2)')
        price.send_keys('100')

        employee = driver.find_element(By.XPATH, '/html/body/div[12]/div/div/form/div/div/div[3]/div[1]/input[1]')
        employee.send_keys('孔银花')
        import time
        time.sleep(2)
        driver.find_element(By.CSS_SELECTOR, '.dropdown-item').click()

        # 时间
        purchase_time = driver.find_element(By.CSS_SELECTOR,
                                            '#addAss-form > div:nth-child(1) > div:nth-child(1) > '
                                            'div:nth-child(3) > div:nth-child(2) > input:nth-child(2)')
        purchase_time.click()
        driver.find_element(By.XPATH, '/html/body/div[9]/div[3]/table/tbody/tr[1]/td[3]').click()

        # 备注
        note = driver.find_element(By.CSS_SELECTOR,
                                   '#addAss-form > div:nth-child(1) > div:nth-child(1) > '
                                   'div:nth-child(4) > div:nth-child(1) > input:nth-child(2)')
        note.send_keys('说明')

        # 资产归属
        owner = driver.find_element(By.CSS_SELECTOR,
                                    '#addAss-form > div:nth-child(1) > div:nth-child(1) > '
                                    'div:nth-child(4) > div:nth-child(2) > select:nth-child(2)')
        owner_size = len(Select(owner).options)
        random_index = random.randint(1, owner_size - 1)
        Select(owner).select_by_index(random_index)

        driver.find_element(By.CSS_SELECTOR,
                            '#addAss > div:nth-child(1) > div:nth-child(1) '
                            '> div:nth-child(3) > button:nth-child(1)').click()

        time.sleep(2)

        # 获取实际结果
        new = DBTools.query(sql)   # 获得添加设备之后的记录数
        if (new[0][0] - old[0][0]) == 1:
            actual = '添加成功'
        else:
            actual = '添加失败'

        # 断言：实际结果和期望结果的对比
        if '添加成功' == actual:
            print('断言成功')
        else:
            print('断言失败')

    def test_query_assets(self):
        pass

    def test_modify_assets(self):
        pass






if __name__ == '__main__':
    tc = TestCase()
    tc.test_add_assets()